package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.Zan;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.mapper.ZanMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ZanMapper zanMapper;

    @Override
    public void save(StrategyComment strategyComment,String[] imgs) {
        //获取当前的用户
        strategyComment.setUser(UserContext.getCurrentUser());
        //创建时间
        strategyComment.setCreateTime(new Date());
        //并接图片的url
        String  url="";
        if(imgs != null && imgs.length>0){
            for (String img : imgs) {
                url=img+";";
            }
        }
        strategyComment.setImgUrls(url);

        strategyCommentMapper.insert(strategyComment);
        String[] tags = strategyComment.getTags();
        Tag tag = new Tag();
        for (String t : tags) {
            tag.setName(t);
            tagMapper.insert(tag);
            strategyCommentMapper.insertCommentAndTagRelation(strategyComment.getId(),tag.getId());
        }

    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentMapper.updateByPrimaryKey(strategyComment);
    }

    @Override
    public void delete(Long id) {
        strategyCommentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public StrategyComment get(Long id) {
        return null;
    }

    @Override
    public List<StrategyComment> listAll() {
        return null;
    }

    @Override
    public PageInfo<StrategyComment> queryCommentByStrategyId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> commentList = strategyCommentMapper.selectCommentByStrategyId(qo);
        for (StrategyComment comment : commentList) {
            //写死点赞类型为攻略评论点赞,
            Zan zan = new Zan();
            zan.setType(2L);
            if (UserContext.getCurrentUser() != null) {
                zan.setUserId(UserContext.getCurrentUser().getId());
            }
            zan.setTypeId(comment.getId());
            comment.setStatus(zanMapper.getZanByCommentId(zan));
            //获取攻略评论点赞的数量
            Zan zan1 = new Zan();
            zan1.setTypeId(comment.getId());
            zan1.setType(2L);
            comment.setCountZan(zanMapper.getZanNumByTravelId(zan1));
        }
        return new PageInfo<>(commentList);
    }

    /**
     * 获取常用标签
     * @param qo : 用pageSize来确定数量
     * @return
     */
    @Override
    public PageInfo getTags(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Map> list = strategyCommentMapper.selectTags(qo);
        return new PageInfo(list);
    }

    /**
     * 查询某个用户的点评
     * @param qo
     * @return
     */
    @Override
    public PageInfo queryCommentsByUserId(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.selectCommentsByUserId(qo);
        return new PageInfo(list);
    }

    @Override
    public StrategyComment getCommentById(Long id) {
        StrategyComment comment = strategyCommentMapper.selectByCommentId(id);
        //获取攻略评论点赞的数量
        Zan zan1 = new Zan();
        zan1.setTypeId(comment.getId());
        zan1.setType(2L);
        comment.setCountZan(zanMapper.getZanNumByTravelId(zan1));
        return comment;
    }
}
